package com.hy.main.demo.io;

import org.junit.Test;

import java.io.*;

/**
 * @Author yang DongYong
 * @Date 2022/4/26 - 04 - 26 - 22:02
 * @Description: com.hy.main.demo.file
 * @version: 1.0
 */
public class BinFileCopy {

    /**
     * 字节流copy 二进制文件
     */
    @Test
    public void copyFile() {
        String destPath = "E:\\桌面\\注解和反射.md";
        String path = "e:\\注解和反射.md";

        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(destPath));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(path));
            // 此处buffer目的起到缓冲作用
            byte[] buffer = new byte[1024 * 1024];
            // 每次读取到的二进制文件长度
            int dataLength;
            while ((dataLength = bufferedInputStream.read(buffer)) != -1) {
                bufferedOutputStream.write(buffer, 0, dataLength);
            }
            System.out.println("拷贝成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedInputStream != null && bufferedOutputStream != null) {
                try {
                    bufferedInputStream.close();
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
